package com.wjh.www.util;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author wjh
 * @date 2021/9/11 20:59
 * @Package com.wjh.anyview.util
 */
public class GetRequestJsonUtil {
    /**
     * 获取request中的参数并转为json
     *
     * @param request req
     * @return 返回json对象
     * @throws IOException io异常
     */
    public static JSONObject getRequestJsonObject(HttpServletRequest request) throws IOException {
        String json = getRequestJsonString(request);
        if (json.contains("&")) {
            json = getJsonStrByQueryUrl(json);
        }
        if (json.contains("=") && !json.contains("&")) {
            json = json + "&";
            json = getJsonStrByQueryUrl(json);
        }
        return JSONObject.parseObject(json);
    }

    /**
     * 把query参数转为json字符串
     *
     * @param paramStr 形如“ a=1314&b=520&c=666 ”
     * @return 返回json字符串
     */
    public static String getJsonStrByQueryUrl(String paramStr) {
        String[] params = paramStr.split("&");
        JSONObject obj = new JSONObject();
        for (String s : params) {
            String[] param = s.split("=");
            if (param.length >= 2) {
                String key = param[0];
                StringBuilder value = new StringBuilder(param[1]);
                for (int j = 2; j < param.length; j++) {
                    value.append("=").append(param[j]);
                }
                try {
                    obj.put(key, value.toString());
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }
        return obj.toString();
    }


    /***
     * 获取 request 中 json 字符串的内容
     *
     * @param request req
     * @return : <code>byte[]</code>
     * @throws IOException io异常
     */
    public static String getRequestJsonString(HttpServletRequest request) throws IOException {
        String submitMehtod = request.getMethod();
        // GET级表单
        if (submitMehtod.equals("GET")) {
            return getJsonStrByQueryUrl(request.getQueryString());
            // POST
        } else {
            return getRequestPostStr(request);
        }
    }

    /**
     * 描述:获取 post 请求的 byte[] 数组
     *
     * @param request req
     * @return 返回byte[]
     * @throws IOException io异常
     */
    public static byte[] getRequestPostBytes(HttpServletRequest request) throws IOException {
        int contentLength = request.getContentLength();
        if (contentLength < 0) {
            return null;
        }
        byte[] buffer = new byte[contentLength];
        for (int i = 0; i < contentLength; ) {

            int readlen = request.getInputStream().read(buffer, i,
                    contentLength - i);
            if (readlen == -1) {
                break;
            }
            i += readlen;
        }
        return buffer;
    }

    /**
     * 描述:获取 post 请求内容
     *
     * @param request req
     * @return 返回处理后的字符串
     * @throws IOException io异常
     */
    public static String getRequestPostStr(HttpServletRequest request) throws IOException {
        byte[] buffer = getRequestPostBytes(request);
        if (buffer == null) {
            return null;
        }
        String charEncoding = request.getCharacterEncoding();
        if (charEncoding == null) {
            charEncoding = "UTF-8";
        }
        return new String(buffer, charEncoding);
    }

}
